"""
Chinese Chess Online
Mai Linh(06TC051), Nguyen The Nhan(06TC074)

Utilities Module
"""
PieceType = ["A","C","R","E","G","K","S"]

class ChessPiece():    
    
    def __init__(self,piecetype,color,pos=None):
        if piecetype not in PieceType:
            raise "Invalid Piece"
        else:
            self._type=piecetype
        if pos:
            x,y = pos
            if ord(x) not in range(ord('a'),ord('i')+1):
                raise "Invalid position"
            if y not in range(1,11):
                raise "Invalid position"
            self._pos = (x,y)
        self._color = color        

    def get_position(self):
        return self._pos
    
    def set_position(self,pos):
        self._pos = pos
    
    def get_color(self):
        return self._color
    
    def set_color(self,color):
        self._color = color
        
    def piecetype(self):
        return self._type
        
    position = property(get_position, set_position)
    color = property(get_color, set_color)
        

class ChessBoard():

    def __init__(self):
        self.board = dict()
        for x in range (ord('a'),ord('i')+1):
            xx = chr(x)
            self.board[xx]=dict()
            for y in range(1,11):
                self.board[xx][y]=None
    
    def set_piece(self,piece,pos):
        x,y = pos
        self.board[x][y]=piece
        piece.position = pos
    
    def get_piece(self,pos):
        x,y = pos
        return self.board[x][y]
    
    def del_piece(self,pos):
        x,y = pos
        self.board[x][y] = None
        
    def move_piece(self,oldpos,newpos):
        piece = self.get_piece(oldpos)
        self.set_piece(piece,newpos)
        self.del_piece(oldpos)
        
        
        